<template>
  <div class="page">
    <avue-crud
      :option="option"
      :search.sync="search"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      :permission="permissionList"
      :before-open="beforeOpen"
      v-model="form"
      ref="crud"
      @row-update="rowUpdate"
      @row-save="rowSave"
      @row-del="rowDel"
      @search-change="searchChange"
      @search-reset="searchReset"
      @selection-change="selectionChange"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @sort-change="sortChange"
      @on-load="onLoad"
    >
      <template slot="menuLeft">
        <el-button
          type="primary"
          size="small"
          icon="el-icon-plus"
          @click="handleAdd"
          >新 增
        </el-button>
        <el-button
          type="danger"
          size="small"
          icon="el-icon-delete"
          plain
          @click="handleDelete"
          >删 除
        </el-button>
      </template>
      <template slot="menuRight"></template>
      <template slot-scope="{}" slot="serviceNoForm">
          <select-page
            ref="searchSerSelect"
            label="serviceName"
            value="serviceNo"
            :select.sync="form.serviceNo"
            :url="website.searchLink.serviceUrl"
            placeholder-text="服务商"
            filter-key="searchParam"
          ></select-page>
      </template>
      <template slot-scope="{}" slot="serviceNoSearch">
          <select-page
            ref="searchSerSelect"
            label="serviceName"
            value="serviceNo"
            :select.sync="search.serviceNo"
            :url="website.searchLink.serviceUrl"
            placeholder-text="服务商"
            filter-key="searchParam"
          ></select-page>
      </template>
    </avue-crud>
  </div>
</template>

<script>
import { getList, add, del } from "@/api/rb-oem/activity/service_deposit";
export default {
  props: {
    serviceArr: {
      type: Array,
      default: () => {
        return [];
      },
    },
    mealCode: {
      type: String,
      default: '',
    }
  },
  data() {
    return {
      screenWidth: document.documentElement.clientWidth,
      data: [],
      search: {},
      form: {},
      query: {},
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
        allPrice: 0,
      },
      props: {
        //菜单权限数据 props
        label: "title",
        value: "key",
      },
      option: {
        //height: "auto",
        calcHeight: 30,
        tip: false,
        border: true,
        index: true,
        viewBtn: false,
        selection: true,
        searchMenuSpan: 6,
        search: false,
        // columnBtn: false,
        addBtn: false,
        editBtn: false,
        column: [
          {
            label: "主键",
            prop: "id",
            type: "input",
            sortable: true,
            addDisplay: false,
            editDisplay: false,
            viewDisplay: false,
            hide: true,
          },
          {
            label: "服务商编号",
            prop: "serviceNo",
            slot: true,
            overHidden: true,
            search: true,
            rules: [
              {
                required: true,
                message: "请输入服务商编号",
                trigger: "blur",
              },
            ],
          },
          // {
          //   label: "服务商编号",
          //   searchLabel: "服务商",
          //   prop: "serviceNo",
          //   type: 'select',
          //   addDisplay: false,
          //   editDisplay: false,
          //   search: true,
          //   span: 24,
          //   overHidden: true,
          //   hide: true,
          //   props: {
          //     label: 'serviceName',
          //     value: 'serviceNo'
          //   },
          //   dicUrl: `/api/rb-oem/oem/pledge/pledge_meal_service/pageList?current=-1&size=-1&mealCode=${this.mealCode}`,
          //   dicFormatter:(data)=>{
          //     return data.data.records
          //   },
          //   rules: [
          //     {
          //       required: true,
          //       message: "请选择服务商",
          //       trigger: "blur",
          //     },
          //   ],
          // },
          {
            label: "",
            prop: "hasChild",
            type: "checkbox",
            multiple: false,
            dataType: "string",
            showColumn: false,
            hide: true,
            dicData: [
              {
                label: "包含下级",
                value: 1
              }
            ]
          },
          {
            label: "服务商名称",
            prop: "serviceName",
            overHidden: true,
            display: false,
          },
          {
            label: "套餐编号",
            prop: "mealCode",
            overHidden: true,
            hide: true,
            display: false,
          },
          {
            label: "创建时间",
            prop: "createTime",
            overHidden: true,
            display: false
          },
        ],
      },
      id: "",
      dialogModel: {
        type: "",
        text: "",
        visible: false,
        subText: "",
        url: "/comppage/getlist/pageServiceName",
        oLabel: "names",
        oValue: "codes",
      },
      modelForm: {
        orgNo: "",
        serviceNo: "",
        hasChild: 0,
      },
      rowType: 'add'
    };
  },
  watch: {
    serviceArr: {
      handler(arr) {
        this.tableData = arr;
        if (arr.length > 0) {
          this.dataList = arr;
        }
      },
      deep: true,
      immediate: true,
    },
  },
  computed: {},
  //   activated () {
  //     this.refreshList()
  //   },
  methods: {
    /**
     * @func 选中单元格
     * @desc 选中单元格数据
     * @param {list} list --- 选中单元格数据-数组
     * */
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1
      this.onLoad(this.page, params);
      done();
    },
    beforeOpen(done, type) {
      if (["add"].includes(type)) {}
      done();
    },
    /**
     * @func 分页页码
     * @desc 分页页码改变时会触发
     * */
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
      // this.refreshChange();
    },
    /**
     * @func 当前页个数
     * @desc pageSize改变时会触发
     * */
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
      // this.refreshChange();
    },
    /**
     * @func 刷新
     * @desc 点击刷新按钮触发该事件
     * */
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    rowSave(row, done, loading) {
      if (this.rowType === 'add') {
        add({...row, 'mealCode': this.mealCode}).then(
          () => {
            this.onLoad(this.page)
            this.$message({
              type: 'success',
              message: '操作成功!'
            })
            done()
          },
          (error) => {
            loading()
          }
        )
      } else if (this.rowType === 'del') {
        del({...row, 'mealCode': this.mealCode}).then(
          () => {
            this.onLoad(this.page)
            this.$message({
              type: 'success',
              message: '操作成功!'
            })
            done()
          },
          (error) => {
            loading()
          }
        )
      }
      
    },
    handleAdd() {
      this.rowType = 'add'
      this.$refs.crud.rowAdd();
    },
    handleDelete() {
      this.rowType = 'del'
      this.$refs.crud.rowAdd();
    },
    rowUpdate(row, index, done, loading) {
      this.$nextTick(() => {
        this.inputForm.productList[index] = Object.assign(
          this.inputForm.productList[index],
          {
            ...row,
          }
        );
      });
      setTimeout(() => {
        done();
      }, 300);
    },
    rowDel(row) {
      this.$confirm('确定将选择数据删除?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          return del({serviceNo: row.serviceNo, mealCode: row.mealCode})
        })
        .then(() => {
          this.onLoad(this.page)
          this.$message({
            type: 'success',
            message: '操作成功!'
          })
        })
    },
    /**
     * @func 初始化
     * @desc 表格初始化查询数据
     * */
    onLoad(page, params = {}) {
      this.loading = true;
      getList(
        page.currentPage,
        page.pageSize,
        Object.assign(params, this.query, {'mealCode': this.mealCode})
      ).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
      //   if (this.awaitData.length > 0) {
      //     //awaitData为全部数据，data是目前表格绑定的数据
      //     this.data = this.awaitData.slice(
      //       (this.page.currentPage - 1) * this.page.pageSize,
      //       this.page.currentPage * this.page.pageSize
      //     );
      //     this.page.total = this.awaitData.length;
      //   }
      //   this.loading = false;
    },
  },
};
</script>

<style scoped>
.z_flex {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
}
</style>